﻿using HospitalFollowUpSystem.DBC;
using HospitalFollowUpSystem.Entity;
using MetroFramework.Forms;
using System;
using System.Windows.Forms;

namespace HospitalFollowUpSystem.View.FrmTopicGroup
{
    public partial class FrmAddTopic : MetroForm
    {
        private String keyTemp, valueTemp;
        public FrmAddTopic(String key, String value)
        {
            InitializeComponent();
            keyTemp = key;
            valueTemp = value;
        }

        //添加选项
        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            var temp = tbChoices.Text;
            if (temp.Length > 0)
            {
                lbChoices.Items.Add(temp);
                tbChoices.Text = "";
            }
            else
            {
                tbChoices.WaterMark = "请输入选项内容";
            }
        }

        //确认
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            var titel = tbTopic.Text;
            if (titel.Length > 0)
            {
                if (lbChoices.Items.Count > 0)
                {
                    Topic topic = new Topic();
                    topic.GroupCode = keyTemp;
                    topic.GroupName = valueTemp;
                    topic.Question = titel;

                    String choicesResult = "";
                    for (int i = 0; i < lbChoices.Items.Count; i++)
                    {
                        choicesResult = choicesResult + lbChoices.Items[i] + "|";
                    }
                    topic.Chioces = choicesResult;
                    
                    if (cmbType.Text.Equals("单选"))
                    {
                        topic.QuestionType = 0;
                    }
                    else
                    {
                        topic.QuestionType = 1;
                    }
                    topic.OperName = "开发";


                    var isSuccess = false;
                    HRCM_DB db = new HRCM_DB();
                    var trans = db.Database.BeginTransaction();
                    try
                    {
                        isSuccess = db.InsertTopic(topic, trans);
                        trans.Commit();
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message, "操作提醒");
                        trans.Rollback();
                    }
                    finally
                    {
                        trans.Dispose();
                    }
                    db.Database.Close();

                    if (isSuccess)
                    {
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "操作提醒");
                    }
                }
                else
                {
                    MessageBox.Show("至少添加一个问题选项");
                }
            }
            else
            {
                tbTopic.WaterMark = "请输入题目描述";
            }
        }

        private void btnESC_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmAddTopic_Load(object sender, EventArgs e)
        {
            cmbType.SelectedIndex = cmbType.Items.IndexOf("单选"); ;
        }
    }


}
